#!/bin/sh -efu
#
# Copyright (C) 2007-2018  Alexey Gladkov <legion@altlinux.org>
# Copyright (C) 2007  Alexey I. Froloff <raorn@altlinux.org>
# Copyright (C) 2008  Dmitry V. Levin <ldv@altlinux.org>
#
# This file is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA.
#

. gear-utils-sh-functions
. shell-getopt

gear_command_method='gear'
show_help()
{
	cat <<-EOF
	Usage: $PROG [options] [$gear_command_method options] [<user>@]<hostname> <workdir>

	<hostname> - remote build host;

	<workdir>  - working directory onto <hostname>.

	Options:
	  --remote-repo         make remote repository and do not download results;
	  --build-only          do not download results onto current host;
	  --cleanup-repo        cleanup hasher workdir on remote host;
	  --commit              make temporary commit prior to extract;
	  --target=ARCH         target architecture;
	  -m, --method=TYPE     defines build method on the remote host (default: gear);
	  -o, --outdir=OUTDIR   download results in DIR on local machine;
	  -r, --rules=FILE      override rules file name;
	  -t, --tree-ish=ID     COMMIT[:PATH] specifying the tree to process;
	  -v, --verbose         print a message for each action;
	  -V,--version          print program version and exit;
	  -h,--help             show this text and exit.

	Report bugs to http://bugzilla.altlinux.org/

	EOF
	exit
}

print_version()
{
	cat <<-EOF
	$PROG version $PROG_VERSION
	Written by Alexey Gladkov <legion@altlinux.org>

	Copyright (C) 2007-2018  Alexey Gladkov <legion@altlinux.org>
	Copyright (C) 2007  Alexey I. Froloff <raorn@altlinux.org>
	Copyright (C) 2008  Dmitry V. Levin <ldv@altlinux.org>
	This is free software; see the source for copying conditions.  There is NO
	warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
	EOF
	exit
}

[ "$#" -gt 0 ] ||
	show_help

case "$PROG" in
	*-hsh) gear_command_method='hsh' ;;
	*-rpm) gear_command_method='rpm' ;;
esac

GETOPT_ALLOW_UNKNOWN=1
GETOPT_ALLOW_ABBREV=
TEMP=`getopt -n $PROG -o m:,o:,r:,t:,v,V,h -l build-only,cleanup,commit,method:,remote-repo,rules:,target:,tree-ish:,outdir:,verbose,version,help -- "$@"` ||
	show_usage
eval set -- "$TEMP"

rules=
do_commit=
main_tree_id=

gear_command_remote_repo=
gear_command_build_only=

while :; do
	case "$1" in
	    --build-only)
		gear_command_build_only=1
		;;
	    --cleanup)
		gear_command_cleanup=1
		;;
	    --commit)
		do_commit=1
		;;
	    --remote-repo)
		[ "$gear_command_method" != 'rpm' ] ||
			show_usage 'Option --method=rpm with --remote-repo does not make sense.'
		gear_command_remote_repo=1
		;;
	    --target) shift
		gear_command_target="$1"
		;;
	    -m|--method) shift
		[ "$1" != 'rpm' ] || [ -z "$gear_command_remote_repo" ] ||
			show_usage 'Option --method=rpm with --remote-repo does not make sense.'
		gear_command_method="$1"
		;;
	    -o|--outdir)
		gear_command_outdir="$(opt_check_dir "$1" "$2")"
		shift
		;;
	    -r|--rules) shift; rules="$1"
		;;
	    -t|--tree-ish) shift; main_tree_id="$1"
		[ -z "$do_commit" ] || [ -z "$main_tree_id" ] ||
			show_usage 'Options --commit and --tree-ish are mutually exclusive.'
		;;
	    -v|--verbose) verbose=-v
		;;
	    -V|--version) print_version
		;;
	    -h|--help) show_help
		;;
	    --) shift; break
		;;
	    *) fatal "Unrecognized option: $1"
		;;
	esac
	shift
done

[ "$#" -ge 2 ] ||
	show_help

args="$verbose"
while [ $# -gt 2 ]; do
	case "$1" in
		--target=*)
			target="${1#--target=}"
			gear_command_target="$(string_quote_remove "$target")"
			;;
	esac
	args="$args \"$(quote_shell "$1")\""
	shift
done

gear_command_server="$1"; shift
gear_command_workdir="$1"; shift

export gear_command_server gear_command_workdir gear_command_method gear_command_target
export gear_command_outdir gear_command_build_only gear_command_cleanup
export gear_command_remote_repo

eval gear \
	$verbose \
	${rules:+-r "$rules"} \
	${do_commit:+--commit} \
	${main_tree_id:+-t "$main_tree_id"} \
	--command \
	-- gear-command-remote-build "$args"
